package com.gy.smart;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentTabHost;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.gy.smart.base.BaseActivity;
import com.gy.smart.util.MainTabEnum;
import com.gy.smart.util.StringConstants;

public class MainActivity extends BaseActivity {
    //定义FragmentTabHost对象
    private FragmentTabHost mTabHost;
    //定义一个布局
    private LayoutInflater layoutInflater;
    private MainTabEnum[] mainTabArray = new MainTabEnum[]{MainTabEnum.HOME, MainTabEnum.HANDLE, MainTabEnum.LIFE, MainTabEnum.TOOL};
    private int pushFlag;
    private boolean isExit = false;

    private int tabIndex = -1;

    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (StringConstants.MAIN_TAB_CHANGE_ACTION.equals(intent.getAction())) {
                int index = intent.getIntExtra(StringConstants.TAB_INDEX, 0);
                mTabHost.setCurrentTab(index);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initData();
        setListener();
        fillData();
    }

    @Override
    protected void initView() {
        setContentViewRes(R.layout.activity_main);
        hidenTop();
    }

    @Override
    protected void initData() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(StringConstants.MAIN_TAB_CHANGE_ACTION);
        filter.addAction(StringConstants.MAIN_TAB_INDEX_ACTION);
        registerReceiver(receiver, filter);
        //实例化布局对象
        layoutInflater = LayoutInflater.from(this);

        //实例化TabHost对象，得到TabHost
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        mTabHost.getTabWidget().setDividerDrawable(R.color.white);
        //得到fragment的个数
        int count = mainTabArray.length;

        for (int i = 0; i < count; i++) {
            //为每一个Tab按钮设置图标、文字和内容
            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mainTabArray[i].name).setIndicator(getTabItemView(i));
            //将Tab按钮添加进Tab选项卡中
            mTabHost.addTab(tabSpec, mainTabArray[i].mClass, null);
            //设置Tab按钮的背景
            mTabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.WHITE);
        }
    }

    @Override
    protected void setListener() {
        super.setListener();
    }


    @Override
    protected void onResume() {
        super.onResume();

        new Handler().post(new Runnable() {
            @Override
            public void run() {
                if(tabIndex != -1){
                    mTabHost.setCurrentTab(tabIndex);
                    tabIndex = -1;
                }
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    /**
     * 给Tab按钮设置图标和文字
     */
    private View getTabItemView(int index) {
        View view = layoutInflater.inflate(R.layout.tab_item_view, null);

        ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
        imageView.setImageResource(mainTabArray[index].imgId);

        TextView textView = (TextView) view.findViewById(R.id.textview);
        textView.setText(mainTabArray[index].name);

        return view;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            ExitProgram();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    public void ExitProgram() {
        if (!isExit) {
            isExit = true;
            showToast("再按一次\"退出\"程序");
            exitHandler.sendEmptyMessageDelayed(0, 2000);
        } else {
            this.finish();
            System.exit(0);
        }
    }

    public Handler exitHandler = new Handler() {
        public void handleMessage(Message msg) {
            isExit = false;
        }
    };
}